///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTURLinkUI.m
//	SUMMARY:	Implementation for a UI for setting an URLRep
//	SUPERCLASS:	eTURLinkUI:eTImageUI:Object
//	INTERFACE:	eTURLinkUI.nib
//	PROTOCOLS:	<Inspectable>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//	IMPLEMENTATION COMMENTS
//		String handling for the text object is stolen from eTImage.
//	Funny that, stealing code from your own superclass :)
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	07/24/94:	Added support for dragging OmniWeb squiggles onto linkView.
//  07/22/94:	Minimal Creation.
///////////////////////////////////////////////////////////////////////////////

#import "eTURLink.h"

@implementation eTURLinkUI
//  id	theeTURLink;
//  id	urlField;
//  id	urlPanel;
//  id	urlView;

+ new
{
	static eTURLinkUI *ui = nil;
	
	if (!ui) {
		ui = [[eTURLinkUI alloc] init];
	}
	return ui;
}
- init
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[eTURLinkUI class]];
	if ( [bundle getPath:buf forResource:"eTURLinkUI" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: eTURLinkUI");
	}
	urlView = [urlPanel contentView];
	[urlField setDelegate:self];
	[urlField setCharFilter:(NXCharFilterFunc)NXFieldFilter];
	[omniWell initOmniWell];
	return self;
}
- free {return self;}
///////////////////////////////////
#define PROP "URL Properties"
- (const NXAtom *) types
{
	static NXAtom *types = NULL;
	
	if (!types) {
		int i;
		const NXAtom *superTypes = [super types];
		
		for(i=0;superTypes[i];i++);
		i++; // make room for terminating NULL
		i++; // make room for our type
		types = malloc(i * sizeof(NXAtom));
		types[0] = NXUniqueString(PROP);
		for(i=0;superTypes[i];i++) types[i+1] = superTypes[i];
		types[i+1] = NULL;
	}
	return types;
}
- (const char *) inspectorTitle
{
	return NXUniqueString("eTURLink");
}
- resignInspector: (View *) oldInspector ofType: (const char *) type
{
	if (oldInspector != urlView)
		[super resignInspector:oldInspector ofType:type];
	return self;
}
- activateInspector: (View *) newInspector ofType: (const char *) type
{
	if (newInspector != urlView)
		[super activateInspector:newInspector ofType:type];
	return self;
}
- inspectorForType:(const char *) type
{
	if(!strcmp(type,NXUniqueString(PROP)))
		return urlView;
	return [super inspectorForType:type];
}
////////////////////
- setAnnotation:sender
{
	[super setAnnotation:sender];	// this is an awkward naming of the "chain" 
	theeTURLink = sender;
	[urlField setText:[theeTURLink URL]];
	return self;
}
- editURL:sender
{
	char		*buf;
	int			length;

	length = [urlField textLength]+1;
	buf = malloc(length * sizeof(char));
	[urlField getSubstring:(buf) start:0 length:length];
	(buf)[length]=0;
	[theeTURLink setURL:buf];
	free(buf);
	return self;
}
- textDidEnd:sender endChar:(unsigned short)whyEnd
{	// should this be checked programatically on setAnnotation:?
	if(urlField==sender){ 
		return [self editURL:sender];
	} else {
		return [super textDidEnd:sender endChar:whyEnd];
	}
	return self;
}
@end